<template>
  <div class="plan-detail-module" v-if="goodsListType">
    <div class="plan-base-wrapper">
      <div class="title">{{ detail.planName }}</div>
      <div class="price">S${{ detail.originalPrice }}</div>
    </div>
    <div class="plan-detail-wrapper">
      <div class="title">Services Include:</div>
      <div class="detail">
        <ul>
            <li v-for="module in detail.modules" :key="module.id">
              <svg-icon class="item-icon" name="dot"></svg-icon>
              <span class="item-name">{{ module.name }}</span>
              <span class="item-price">(S${{ module.price }})</span>
            </li>
          </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    goodsList: {
      type: Array,
      default: function () {
        return [
          {
            productType: "plan",
            productId: "2233",
            planDetail: {
              name: "",
              country: "",
              summary: "",
              priceSg: "",
              moduleList: [
                {
                  country: "",
                  name: "",
                  summary: "",
                  priceSg: "",
                },
              ],
            },
            moduleDetail: {
              country: "",
              name: "",
              summary: "",
              priceSg: "",
            },
          },
        ];
      },
    },
  },
  data() {
    return {};
  },
  computed: {
    goodsListType() {
      const goodsList = this.goodsList;
      if (goodsList && goodsList.length > 0) {
        return goodsList[0].productType === "plan" ? 1 : 2;
      } else {
        return undefined;
      }
    },
    detail() {
      const goodsList = this.goodsList;
      if (this.goodsListType === 1) {
        const planDetail = goodsList[0].planDetail;
        const modules = planDetail.moduleList;
        return {
          planName: planDetail.name,
          summary: planDetail.summary,
          price: this.sumPrice(modules),
          originalPrice: this.sumPrice(modules, "originalPrice"),
          modules: modules,
        };
      } else {
        const modules = goodsList.map(i => i.moduleDetail);
        return {
          planName: "Custom",
          price: this.sumPrice(modules),
          originalPrice: this.sumPrice(modules, "originalPrice"),
          modules: modules,
        };
      }
    },
  },
  methods: {
    sumPrice(dataList, field = "price") {
      return dataList.map(i => +i[field]).reduce((a, b) => a + b, 0);
    },
  },
};
</script>

<style lang="scss" scoped>
.plan-detail-module {
  width: 1200px;
  display: flex;
  justify-content: space-between;
  padding: 35px 40px;
  border: 1px solid #eee;
  border-radius: 4px;
}
.plan-base-wrapper {
  width: 500px;
  color: #333;

  .title {
    font-size: 20px;
    font-weight: bolder;
  }

  .price {
    color: #ff8653;
    font-size: 20px;
    font-weight: bolder;
    .origin {
      font-weight: normal;
      font-size: 14px;
      color: grey;
      text-decoration: line-through;
    }
  }
}

.plan-detail-wrapper {
  .title {
    font-weight: bold;
    // color: grey;
    font-size: 17px;
  }
  .detail {
    width: 500px;
    display: flex;
    justify-content: space-between;

    ul {
      list-style: none;
      padding: 0;
      margin: 0;
      // font-size: 16px;
      display: inline-block;
      line-height: 20px;
      .item-icon {
        vertical-align: middle;
        color: grey;
        margin-top: 5px;
      }
      .item-name {
        vertical-align: middle;
        font-size: 16px;
      }
      .item-price {
        vertical-align: middle;
        white-space: nowrap;
        padding-left: 35px;
        color: #666;
      }
    }
  }
}
</style>
